import request from '../utils/request';
import service from './service';

// 基础响应类型
export interface BaseResponse<T = any> {
  code?: number;
  data?: T;
  message?: string;
  trace_id?: string;
  [property: string]: any;
}

// 课题状态枚举
export enum ProjectStatus {
  Closed = "closed",
  Completed = "completed",
  InProgress = "in_progress",
  Open = "open",
}

// 申请状态枚举
export enum ApplicationStatus {
  Accepted = "accepted",
  Pending = "pending",
  Rejected = "rejected",
  Withdrawn = "withdrawn",
}

// 课题类型
export interface Project {
  id: number;
  title: string;
  description: string;
  requirements: string;
  budget: number;
  deadline: string;
  category: string;
  tags: string;
  status: ProjectStatus;
  contact_info?: string;
  is_public: boolean;
  view_count: number;
  apply_count: number;
  priority: number;
  location: string;
  skills: string;
  duration: number;
  max_applicants: number;
  current_applicants: number;
  start_date?: string | null;
  end_date?: string | null;
  creator_id: number;
  created_at: string;
  updated_at: string;
  creator?: {
    id: number;
    username: string;
    real_name: string;
    role: string;
    email: string;
    status: string;
  };
  applications?: any[];
  matched_users?: any[];
}

// 申请类型
export interface Application {
  id: number;
  project_id: number;
  applicant_id: number;
  status: ApplicationStatus;
  cover_letter: string;
  resume: string;
  portfolio: string;
  research_plan?: string;
  motivation?: string;
  qualifications?: string;
  expected_time?: number;
  start_date?: string;
  end_date?: string;
  reviewer_id?: number | null;
  review_comment?: string;
  review_date?: string | null;
  apply_date: string;
  created_at: string;
  updated_at: string;
  project?: {
    id: number;
    title: string;
    description: string;
    status: string;
    creator_id: number;
    budget: number;
    deadline: string;
    category: string;
  };
  applicant?: {
    id: number;
    username: string;
    real_name: string;
    role: string;
    email: string;
    status: string;
  };
  reviewer?: {
    id: number;
    username: string;
    real_name: string;
    role: string;
  } | null;
}

// 创建课题请求参数
export interface CreateProjectRequest {
  title: string;                    // 必填
  description: string;              // 必填
  requirements: string;             // 必填
  budget: number;                   // 必填，必须大于0
  deadline: string;                 // 必填
  category: string;                 // 必填
  tags: string[];                   // 必填
  contact_info?: string;            // 可选
  is_public?: boolean;              // 可选，默认true
}

// 更新课题请求参数
export interface UpdateProjectRequest {
  title?: string;                    // 可选，课题标题
  description?: string;              // 可选，课题描述
  requirements?: string;             // 可选，需求描述
  budget?: number;                   // 可选，预算，必须大于0
  deadline?: string;                 // 可选，截止日期
  category?: string;                 // 可选，课题分类
  tags?: string[];                   // 可选，课题标签数组
  contact_info?: string;             // 可选，联系方式
  is_public?: boolean;               // 可选，是否公开
}

// 更新申请状态请求参数
export interface UpdateApplicationStatusRequest {
  status: ApplicationStatus;
  feedback: string;
  [property: string]: any;
}

// API方法
// 获取所有课题列表（用于浏览）
export const getAllProjects = (params?: {
  page?: number;
  size?: number;
  keyword?: string;
  category?: string;
  status?: string;
  budget_min?: number;
  budget_max?: number;
  is_public?: string;
}): Promise<BaseResponse<Project[]>> => {
  return service.get('/projects', { params });
};

// 获取我的课题列表
export const getMyProjects = (params?: { page?: number; size?: number }): Promise<{
  code: number;
  message: string;
  data: Project[];
  total: number;
  page: number;
  size: number;
  pages: number;
  trace_id?: string;
}> => {
  return service.get('/projects/my', { params });
};

export const getProjectDetail = (id: number): Promise<BaseResponse<Project>> => {
  return request({
    url: `/projects/${id}`,
    method: 'get'
  });
};

export const createProject = (data: CreateProjectRequest): Promise<BaseResponse<Project>> => {
  return service.post('/projects', data);
};

export const updateProject = (id: number, data: UpdateProjectRequest): Promise<BaseResponse<Project>> => {
  return service.put(`/projects/${id}`, data);
};

export const deleteProject = (id: number): Promise<BaseResponse<{
  deleted_project_id: number;
  deleted_at: string;
}>> => {
  return service.delete(`/projects/${id}`);
};

export const getApplications = (params?: {
  project_id?: number;
  page?: number;
  size?: number;
  status?: string
}): Promise<{
  code: number;
  message: string;
  data: Application[];
  total: number;
  page: number;
  size: number;
  pages: number;
  trace_id?: string;
}> => {
  return service.get('/applications', { params });
};

export const getApplicationDetail = (id: number): Promise<BaseResponse<Application>> => {
  return service.get(`/applications/${id}`);
};

export const updateApplicationStatus = (
  id: number,
  data: UpdateApplicationStatusRequest
): Promise<BaseResponse> => {
  return service.put(`/applications/${id}/status`, data);
};


